package com.fox0g.service.impl;

import com.fox0g.mapper.FarmMapper;
import com.fox0g.mapper.FreshfoodMapper;
import com.fox0g.netty.NettyServerHandler;
import com.fox0g.pojo.Farm;
import com.fox0g.service.FarmService;
import com.fox0g.service.QueuecountService;
import com.fox0g.websocket.WebSocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Queue;

/**
* @author 86176
* @description 针对表【queuecount(排队人数统计)】的数据库操作Service实现
* @createDate 2024-07-04 10:34:25
*/
@Service
public class QueuecountServiceImpl implements QueuecountService{
    ArrayList<String> list = new ArrayList<>();
    //    @Autowired
//    ShengxiangService shengxiangService;
    @Autowired
    FarmService farmService;

    @Autowired
    FarmMapper farmMapper;



    @Autowired
    WebSocketHandler webSocketHandler;
    @Autowired
    NettyServerHandler nettyServerHandler;
    @Autowired
    FreshfoodMapper freshfoodMapper;


    public static String Meg = "";

    @Override
    public void HandleMessage(String message) throws IOException {


        webSocketHandler.send(message);
//        解析温湿度光照强度数据帧
        if (message.matches("HWt[0-9]+.[0-9]h[0-9]+.[0-9]ls[0-9]+.[0-9]Z")) {

            String arrnews1[] = message.split("t");
            String arrnew2[] = arrnews1[1].split("h");
            String arrnew3[] = arrnew2[1].split("ls");
            String arrnew4[] = arrnew3[1].split("Z");
            String T = arrnew2[0];  //截取温度
            String H = arrnew3[0];  //截取湿度
            String LS = arrnew4[0]; //截取光敏度
            LocalDate now = LocalDateTime.now().toLocalDate(); //获取当前时间
            //向前端发送这些数据帧
            webSocketHandler.send("t" + arrnew2[0]);
            webSocketHandler.send("Hd" + arrnew3[0]);
            webSocketHandler.send("ls" + arrnew4[0]);
            //将解析后的数据设置给pojo类
//            farm.setTState(T);
//            farm.setHState(H);
//            farm.setLsState(LS);
//            farm.setCreateTime(now);
            Farm farm = new Farm();
            farm.setTstate(T);
            farm.setHstate(H);
            farm.setLsstate(LS);
            farm.setCreateTime(now);
            farmMapper.insert(farm);


            if (Meg != message) {
                farmMapper.insert(farm);
                Meg = message;
            }
        }


//        //以下为排队机内容
//        String cardmatch = "HWPID[0-9A-Z]+Z";
//        if (message.matches(cardmatch)) {
//            String card = message.substring(5, message.length() - 1);
//            Queue queue = new Queue();
//            LocalTime now = LocalDateTime.now().toLocalTime();
//            queue.setQueuenum(card);
//            queue.setCreateTime(now);
//            queueMapper.insert(queue);
//
//        }

    }



}




